program main
  use mpi
  implicit none
  integer rank, size, tag, ierr
  integer senddata, recvdata
  integer status(MPI_STATUS_SIZE)

  call MPI_INIT(ierr) ! 初始化 MPI 环境
  call MPI_COMM_RANK(MPI_COMM_WORLD, rank, ierr) ! 返回该进程进程号
  call MPI_COMM_SIZE(MPI_COMM_WORLD, size, ierr) ! 返回总进程数

  if(rank == 0) then
      senddata = 999
      ! 从 rank0 向 rank1 发送一个长度为 1 的整形数据
      call MPI_SEND(senddata,1,MPI_INTEGER,rank+1,1,MPI_COMM_WORLD,ierr)
      print *, 'Process ', rank, ' send', senddata,' to Process ', rank+1
  endif
  if(rank==1) then
      ! rank1 从 rank0 接收一个长度为 1 的整形数据
      call MPI_RECV(recvdata,1,MPI_INTEGER,rank-1,1,MPI_COMM_WORLD,status,ierr)
      print *, 'Process', rank,' receive ', recvdata, ' from Process' , rank-1
      print *, 'Process', rank,' receive ', recvdata, ' from Process' , status(MPI_SOURCE)
  endif

  call MPI_FINALIZE(ierr)
end program main